package com.sales.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sales.client.StaffSalesClient;


import com.sales.dao.QuotationDetMapper;
import com.sales.dao.QuotationMapper;
import com.sales.service.QuotationService;

import org.comple.erp.pojo.base.enterprise.Staff;
import org.comple.erp.pojo.sales.Quotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class QuotationServiceImpl implements QuotationService {

    @Autowired
    private QuotationMapper quotationMapper;

    @Autowired
    private QuotationDetMapper quotationDetMapper;

    @Autowired
    private StaffSalesClient staffSalesClient;
    @Override
    public PageInfo<Quotation> quotationList(Map<String, Object> params) {

        String code = (String) params.get("code");
        if(code != null && !code.isEmpty()){
            String workNumber = code;
            if (workNumber != null) {
                params.put("workNumber", workNumber);
            }
            params.remove("code");
        }

        String pageNumStr = (String) params.get("pageNum");
        String pageSizeStr = (String) params.get("pageSize");

        int pageNum = pageNumStr != null && !pageNumStr.isEmpty() ? Integer.parseInt(pageNumStr) : 1;
        int pageSize = pageSizeStr != null && !pageSizeStr.isEmpty() ? Integer.parseInt(pageSizeStr) : 5;

        PageHelper.startPage(pageNum, pageSize);
        //销售人员
        List<Quotation> quotationList = quotationMapper.quotationList(params);

//        List<Staff> staffList = staffSalesClient.StaffSalesList(params);
//        if (staffList != null) {
//            for (int i = 0; i < quotationList.size(); i++) {
//                for (int j = 0; j < staffList.size(); j++) {
//                    if (quotationList.get(i).getWorkNumber() == staffList.get(j).getCode()) {
//                        quotationList.get(i).setStaffName(staffList.get(j).getName());
//                        break;
//                    }
//                }
//            }
//        }

        //

        PageInfo<Quotation> pageInfo = new PageInfo<>(quotationList,5);
        return pageInfo;
    }

    @Override
    public Boolean insertSelective(Quotation quotation) {

        if(quotationMapper.insertSelective(quotation)>0 ){
            return true;
        }
        return false;
    }

    @Override
    public Quotation selectByPrimaryKey(String id) {
        return quotationMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean updateByPrimaryKeySelective(Quotation quotation) {
        if(quotationMapper.updateByPrimaryKeySelective(quotation) > 0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteByPrimaryKey(String id) {
        String code = quotationMapper.selectByPrimaryKey(id).getCode();
        if(quotationDetMapper.selectByQuotationCode(code) != null){
            quotationDetMapper.deleteByCodeLj(code);
        }
        if(quotationMapper.deleteByPrimaryKey(id) > 0){
            return true;
        }
        return false;
    }


}
